package com.das.mechanic_base.utils;

import com.das.mechanic_base.bean.create.SelectWorkBean;
import com.das.mechanic_base.bean.main.CarBrandBean;
import com.das.mechanic_base.bean.main.CarPoolBean;
import com.das.mechanic_base.bean.main.CrmCarQuetionBean;

import com.github.promeg.pinyinhelper.Pinyin;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class LanguageConvent {

    /**
     * 返回中文拼音及英文大写，
     *
     * @return
     */
    public static String getPinYin(String input) {
        List<X3HanziToPinyin.Token> tokens = X3HanziToPinyin.getInstance().get(input);
        StringBuilder sb = new StringBuilder();
        if (tokens != null && tokens.size() > 0) {
            for (X3HanziToPinyin.Token token : tokens) {
                if (X3HanziToPinyin.Token.PINYIN == token.type) {
                    sb.append(token.target);
                } else {
                    sb.append(token.source);
                }
            }
        }else{
            //如果获取不到实例，则返回一个特殊字符
            sb.append("#");
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 获取名字对应的首字母字符串
     * 规则：
     * 当名称为中文开头时，返回名称第一个汉字的拼音的首字母的大写
     * 当名称为英文开头时，返回第一次英文字母的大写
     * 当名称为数字开头或者是其他特殊符号时，返回字符串“其他”
     */
    public String getFirstChar(String name) {
        if (name != null && name.length() > 0) {
            String firstChar = LanguageConvent.getPinYin(name).charAt(0) + "";
            if (Pattern.compile("[a-zA-Z]").matcher(firstChar).matches()) {//是字符
                return firstChar + "";
            } else {
                return "#";
            }
        } else {
            return " ";
        }
    }

    /**
     * 为ListView填充数据
     * @param mList
     * @return
     */
    public static List<CarBrandBean> filledData(List<CarBrandBean> mList){
        List<CarBrandBean> mSortList = new ArrayList<>();

        for(int i = 0; i < mList.size(); i++){
            CarBrandBean carBrandBean = mList.get(i);

            String label = carBrandBean.getLabel();
            if (X3StringUtils.isEmpty(label)) {
                carBrandBean.setSortLetters("#");
                mSortList.add(carBrandBean);
                continue;
            }
            //汉字转换成拼音
            String sortString;
            if (LanguageUtiles.isZhRCN()) {
                sortString = (Pinyin.toPinyin(String.valueOf(label.charAt(0)),"/").charAt(0) + "").toUpperCase();
            }else {
                sortString = label.substring(0, 1).toUpperCase();
            }

            // 正则表达式，判断首字母是否是英文字母
            if(sortString.matches("[A-Z]")){
                carBrandBean.setSortLetters(sortString.toUpperCase());
            }else{
                carBrandBean.setSortLetters("#");
            }

            mSortList.add(carBrandBean);
        }
        return mSortList;

    }

    /**
     * 为选择施工项目填充数据
     * @param mList
     * @return
     */
    public static List<SelectWorkBean.ServiceBaseEntityBean> sortWorkData(List<SelectWorkBean.ServiceBaseEntityBean> mList){
        List<SelectWorkBean.ServiceBaseEntityBean> mSortList = new ArrayList<>();

        for(int i = 0; i < mList.size(); i++){
            SelectWorkBean.ServiceBaseEntityBean bean = mList.get(i);

            String label = bean.name;
            if (X3StringUtils.isEmpty(label)) {
                bean.sortLetters= "#";
                mSortList.add(bean);
                continue;
            }
            //汉字转换成拼音
            String sortString;
            if (LanguageUtiles.isZhRCN()) {
                sortString = (Pinyin.toPinyin(String.valueOf(label.charAt(0)),"/").charAt(0) + "").toUpperCase();
            }else {
                sortString = label.substring(0, 1).toUpperCase();
            }

            // 正则表达式，判断首字母是否是英文字母
            if(sortString.matches("[A-Z]")){
                bean.setSortLetters(sortString.toUpperCase());
            }else{
                bean.setSortLetters("#");
            }

            mSortList.add(bean);
        }
        return mSortList;

    }
    public static List<CrmCarQuetionBean.SubItemList> filledDatas(List<CrmCarQuetionBean.SubItemList> mList){//车况问题
        List<CrmCarQuetionBean.SubItemList> mSortList = new ArrayList<>();
        for(int i = 0; i < mList.size(); i++){
            CrmCarQuetionBean.SubItemList subItemList = mList.get(i);

            String itemName = subItemList.itemName;
            if (X3StringUtils.isEmpty(itemName)) {
                subItemList.sortLetters = "#";
                mSortList.add(subItemList);
                continue;
            }
            //汉字转换成拼音
            String sortString;
            if (LanguageUtiles.isZhRCN()) {
                sortString = (Pinyin.toPinyin(String.valueOf(itemName.charAt(0)),"/").charAt(0) + "").toUpperCase();
            }else {
                sortString = itemName.substring(0, 1).toUpperCase();
            }

            // 正则表达式，判断首字母是否是英文字母
            if(sortString.matches("[A-Z]")){
                subItemList.sortLetters = sortString.toUpperCase();
            }else{
                subItemList.sortLetters = "#";
            }

            mSortList.add(subItemList);
        }
        return mSortList;

    }
    public static List<CarPoolBean> filledCarPool(List<CarPoolBean> mList){//车况问题
        List<CarPoolBean> mSortList = new ArrayList<>();

        for(int i = 0; i < mList.size(); i++) {
            CarPoolBean carPoolBean = mList.get(i);

            String label = carPoolBean.label;
            if (X3StringUtils.isEmpty(label)) {
                carPoolBean.sortLetters = "#";
                mSortList.add(carPoolBean);
                continue;
            }
            //汉字转换成拼音
            String sortString;
            if (LanguageUtiles.isZhRCN()) {
                sortString = (Pinyin.toPinyin(String.valueOf(label.charAt(0)),"/").charAt(0) + "").toUpperCase();
            }else {
                sortString = label.substring(0, 1).toUpperCase();
            }

            // 正则表达式，判断首字母是否是英文字母
            if(sortString.matches("[A-Z]")){
                carPoolBean.sortLetters = sortString.toUpperCase();
            }else{
                carPoolBean.sortLetters = "#";
            }

            mSortList.add(carPoolBean);
        }
        return mSortList;


    }
}
